package Client.GUI;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import Client.logic.ClientIF;
import Client.logic.MyFile;
import Server.DataBase.Lesson;
import Server.Message.MessageGetStudentLesson;
import Server.Message.MessageGetTaskId;
import Server.Message.MessageGetTaskIdReplay;
import Server.Message.MessageLessonReplay;
import Server.Message.MessageSubmitTask;
import Server.Message.MessageSubmitTaskReplay;

public class SubmitTaskPanel extends MyJPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JComboBox comboBoxLesson, comboBoxTask;
	private Lesson lesson;
	private int taskId;
	private JButton btnChooseDir;
	private String dir, filename;
	private JButton btnUpload;

	public SubmitTaskPanel(ClientIF client) {
		super(PanelType.GET_TASK, client);
		
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"submit task.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(27, 39, 294, 99);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setLayout(null);
		init();
	}
	
	public void init(){
		initLabels();
		initComboBox();
		initBtn();
		
		getClient().sendMsgToServer(new MessageGetStudentLesson(getClient().getUser().getIdUser()));
		MessageLessonReplay msg = (MessageLessonReplay) getClient().getMessageFromServer();
		
		comboBoxLesson.addItem("choose");
		
		JLabel lblFile = new JLabel("File:");
		lblFile.setBounds(54, 275, 46, 14);
		add(lblFile);
		
		for (Lesson lesson : msg.getArray()) comboBoxLesson.addItem(lesson);
		comboBoxLesson.addActionListener(new LessonListener());
	}

	public void initLabels(){
		JLabel lblLesson = new JLabel("Lesson:");
		lblLesson.setBounds(54, 171, 59, 14);
		add(lblLesson);
		
		JLabel lblTask = new JLabel("Task:");
		lblTask.setBounds(54, 226, 46, 14);
		add(lblTask);
	}
	
	public void initComboBox(){
		comboBoxLesson = new JComboBox();
		comboBoxLesson.setBounds(123, 168, 111, 20);
		add(comboBoxLesson);
		
		comboBoxTask = new JComboBox();
		comboBoxTask.setBounds(123, 223, 111, 20);
		comboBoxTask.setEnabled(false);
		add(comboBoxTask);
	}
	
	public void initBtn(){
		btnChooseDir = new JButton("Choose");
		btnChooseDir.addActionListener(new ChooseFile());
		btnChooseDir.setEnabled(false);
		btnChooseDir.setBounds(123, 271, 111, 23);
		add(btnChooseDir);
		
		btnUpload = new JButton("Upload");
		btnUpload.setBounds(114, 321, 133, 35);
		btnUpload.setEnabled(false);
		btnUpload.addActionListener(new SendFile());
		add(btnUpload);
	}
	
	private class LessonListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				lesson = (Lesson) comboBoxLesson.getSelectedItem();	
			
				getClient().sendMsgToServer(new MessageGetTaskId(lesson));
				MessageGetTaskIdReplay msg = (MessageGetTaskIdReplay) getClient().getMessageFromServer();
			
				if (msg.getMaxid()!=0){
					for (int i=1; i<msg.getMaxid(); i++) comboBoxTask.addItem(i);
					comboBoxTask.setEnabled(true);
					comboBoxTask.addActionListener(new TaskListener());
				}
			} catch (java.lang.Throwable t){}
		}
	}
	
	private class TaskListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				taskId =  (Integer) comboBoxTask.getSelectedItem();
				btnChooseDir.setEnabled(true);
				comboBoxTask.setEnabled(false);
			} catch (java.lang.Throwable t){}
			
		}
	}
	
	class ChooseFile implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JFileChooser c = new JFileChooser();
			int rVal = c.showOpenDialog((Component) getClient());
			if (rVal == JFileChooser.APPROVE_OPTION) {
				filename=c.getSelectedFile().getName();
				dir=c.getCurrentDirectory().toString();
				btnUpload.setEnabled(true);
			}
			
	    }
	 }
	
	 class SendFile implements ActionListener{
		 public void actionPerformed(ActionEvent e) {
			 String LocalfilePath=dir+File.separator+filename;
			 try{
				 File newFile = new File (LocalfilePath);  		      
			     byte [] mybytearray  = new byte [(int)newFile.length()];
			     FileInputStream fis = new FileInputStream(newFile);
			     BufferedInputStream bis = new BufferedInputStream(fis);			  
			     bis.read(mybytearray,0,mybytearray.length);
			     MyFile msg= new MyFile(filename, mybytearray);
			     Calendar cal = Calendar.getInstance();
			     int month = cal.get(Calendar.MONTH)+1;
			     int day = cal.get(Calendar.DAY_OF_MONTH);
			     int year = cal.get(Calendar.YEAR);
			     getClient().sendMsgToServer(new MessageSubmitTask(lesson.getIdLesson(), taskId, getClient().getUser().getIdUser(),
			    		 day, month, year, msg));	
			     MessageSubmitTaskReplay msg2 = (MessageSubmitTaskReplay) getClient().getMessageFromServer();
			     String str;
			     if (msg2.isOnTime()==0) str = "Not submitted on time";
			     else str = "submitted on time";
			     JOptionPane.showMessageDialog((Component) getClient(), str);
			     getClient().swapFromBack(new SubmitTaskPanel(getClient()));
			    }
			catch (Exception e1) {
				e1.printStackTrace();
			}
		 }
	 }
		 
	 @Override
	 public MyJPanel pushPanel() {
		 return new SubmitTaskPanel(getClient());
	 }
}
